/*
 *
 */

package com.hlingsoft.kkh.action.v11;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;

import com.hlingsoft.kkh.entity.vo.EventsVo;
import com.hlingsoft.kkh.entity.vo.StartsVo;

import com.hlingsoft.kkh.util.Common;
import com.hlingsoft.kkh.util.EventsVoSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 职位
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/v11/extension")

public class ExtensionAction_V11 extends BaseAction {


    @Autowired
    private EventsDao eventsDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private EventCommentsDao eventCommentsDao;

    @Autowired
    private FocusDao focusDao;

    @Autowired
    private ImageDao imageDao;

    @Autowired
    private LikesDao likesDao;

    @Autowired
    private ShareDao shareDao;

    private static final int rows = 10;

    /**
     * 附近和最列表
     *  最新创建 1
     火热互动 2
     分享最多 3
     评论最多 4
     * @param userId
     * @param type
     * @param latitude
     * @param longitude
     * @param request
     * @return
     */
    @RequestMapping(value = "/rim.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> checkIn(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "type",defaultValue = "0",required = false) int type,   //1最新创建  2火热互动   3分享最多  4点赞最多  5评论最多 6好友动态
            @RequestParam(value = "latitude") double latitude,
            @RequestParam(value = "longitude") double longitude,
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Events> eventsList = new ArrayList<Events>();
            if (type==1){
                eventsList = eventsDao.getNewestEvents((pageNum-1)*rows,rows);
            } else if (type==2)  {
//                eventsList=eventsDao.getHotEvents(1,5);
//                if (eventsList.size()<5){
//                    eventsList.addAll(eventsDao.getHotEventsDefault(0,5-eventsList.size()));
//                }

                eventsList= eventsDao.getHotEventsDefault((pageNum-1)*rows,rows);
            }else if(type==3){
//                eventsList = eventsDao.getMostShare(1,5);
//                if (eventsList.size()<5){
//                    eventsList.addAll(eventsDao.getMostShareDefault(0,5-eventsList.size()));
//                }
                eventsList=eventsDao.getMostShareDefault((pageNum-1)*rows,rows);
            }else if (type==4){
//                eventsList = eventsDao.getMostLikes(0, 5);
                eventsList = eventsDao.getMostLikes((pageNum-1)*rows,rows);
            }
            else if (type==5){
//                eventsList = eventsDao.getMostEventComments(0, 5);
                eventsList = eventsDao.getMostEventComments((pageNum-1)*rows,rows);
            }else if(type==6){
                //获取好友
                List<Focus> focusList = focusDao.getAttentionsById(userId,0,100);
                for (Focus focus:focusList) {
                    AppUser user = userDao.getUserById(focus.getAttentionsId());
                    List<Events> events = eventsDao.getEventsByUserId(user.getId());
                    if (events!=null){
                        eventsList.addAll(events);
                    }
                }
                eventsList=Common.fenYe(eventsList,rows*(pageNum-1),rows);
            } else {
//                eventsList = eventsDao.getNearlyEvents(userId, latitude, longitude, rows*(pageNum-1), rows);
                eventsList = eventsDao.getAllEvents();
                for (Events events:eventsList){
                    events.setDistance(Common.getDistance(events.getLongitude(),events.getLatitude(),longitude,latitude));
                }
                Collections.sort(eventsList, new Comparator<Events>() {
                    @Override
                    public int compare(Events o1, Events o2) {
                        return o1.getDistance().compareTo(o2.getDistance());
                    }
                });
                eventsList=Common.fenYe(eventsList,rows*(pageNum-1),rows);
            }
            List<EventsVo> resultList = new ArrayList<EventsVo>();
            for (Events events : eventsList) {
                EventsVo eventsVo = new EventsVo();
                eventsVo.setId(events.getId());
                eventsVo.setName(events.getName());

                //图片处理
                eventsVo.setImages(events.getImages());

                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
                eventsVo.setImageList(imageList);

                eventsVo.setDescription(events.getDescription());
                eventsVo.setAppUserId(events.getAppUserId());
                eventsVo.setCreateTime(events.getCreateTime());
                eventsVo.setUpdateTime(events.getUpdateTime());
                eventsVo.setCreateAddress(events.getCreateAddress());
                eventsVo.setLatitude(events.getLatitude());
                eventsVo.setLongitude(events.getLongitude());
                eventsVo.setUpdateTime(events.getUpdateTime());

                eventsVo.setAppUser(userDao.getUserById(events.getAppUserId()));
                resultList.add(eventsVo);
            }

            if (type==6||type==0||type==3||type==2){
                EventsVoSort sort = new EventsVoSort();
                Collections.sort(resultList,sort);
            }

            resultMap.put("resultList", resultList);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 最新发布
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/newestCommentList.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> newestCommentList(
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            HttpServletRequest request) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<EventsVo> newestCommentList = new ArrayList<EventsVo>();
            List<EventComments> eventCommentsList = eventCommentsDao.getNewestEventComments((pageNum-1)*rows,rows);

            for (EventComments newestEventComments:eventCommentsList){
                Events events = eventsDao.getEventsById(newestEventComments.getEventId());

                EventsVo newestCommentEventsVo = new EventsVo();
                //events基本信息
                newestCommentEventsVo.setId(events.getId());
                newestCommentEventsVo.setName(events.getName());

                //活动图片定义为评论的图片
                List<Image> imageList1 = imageDao.getImageByEventCommentsId(newestEventComments.getId());
                newestCommentEventsVo.setImageList(imageList1);
                newestCommentEventsVo.setAppUserId(newestEventComments.getAppUserId());  //用户信息用评论人的信息
                newestCommentEventsVo.setDescription(newestEventComments.getContent());  //说两句用评论人的评论


                newestCommentEventsVo.setCreateTime(newestEventComments.getCreateTime());
                newestCommentEventsVo.setCreateAddress(events.getCreateAddress());
                newestCommentEventsVo.setLatitude(events.getLatitude());
                newestCommentEventsVo.setLongitude(events.getLongitude());
                newestCommentEventsVo.setUpdateTime(newestEventComments.getUpdateTime());
                //最新发布赞
                newestCommentEventsVo.setNumber(likesDao.getLikesByEventsIdCount(newestCommentEventsVo.getId()));


                newestCommentEventsVo.setAppUser(userDao.getUserById(newestEventComments.getAppUserId()));
                newestCommentList.add(newestCommentEventsVo);
            }

            EventsVoSort sort = new EventsVoSort();
            Collections.sort(newestCommentList,sort);

            resultMap.put("result", true);
            resultMap.put("newestCommentList",newestCommentList);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



    /**
     * 开开红人
     *
     * @param userId
     * @param latitude
     * @param longitude
     * @param request
     * @return
     */
    @RequestMapping(value = "/kkStarts.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> kkStart(
            @RequestParam(value = "userId", defaultValue = "0") long userId,
            @RequestParam(value = "latitude") String latitude,
            @RequestParam(value = "longitude") String longitude,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<StartsVo> resultList = new ArrayList<StartsVo>();

            //获取点赞最多人
            List<AppUser> appUserList = userDao.getMostLikeUser();
            for (AppUser user : appUserList) {
                //获取红人粉丝数
                long fansCount = focusDao.getFansByIdCount(user.getId());
                user.setFansCount(fansCount);


                //是否已关注红人
                if (focusDao.getFocus(userId, user.getId()) == null) {
                    user.setIsFocus(0);
                } else {
                    user.setIsFocus(1);
                    if (focusDao.getFocus(user.getId(), userId) != null) {
                        user.setIsFocus(2);
                    }
                }

                Events events = eventsDao.getNewestEventsByUserId(user.getId());
                StartsVo startsVo = new StartsVo();
                startsVo.setAppUser(user);
                startsVo.setNewestEvents(events);
                resultList.add(startsVo);
            }
            resultMap.put("resultList", resultList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     * 所有红人
     *
     * @param userId
     * @param latitude
     * @param longitude
     * @param request
     * @return
     */
    @RequestMapping(value = "/allStarts.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> allStarts(
            @RequestParam(value = "userId", defaultValue = "0") long userId,
            @RequestParam(value = "latitude") String latitude,
            @RequestParam(value = "longitude") String longitude,
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<StartsVo> resultList = new ArrayList<StartsVo>();

            List<AppUser> appUserList = userDao.getAll(rows*(pageNum-1),rows);
            for (AppUser user : appUserList) {
                //获取红人粉丝数
                long fansCount = focusDao.getFansByIdCount(user.getId());
                user.setFansCount(fansCount);


                //是否已关注红人
                if (focusDao.getFocus(userId, user.getId()) == null) {
                    user.setIsFocus(0);
                } else {
                    user.setIsFocus(1);
                    if (focusDao.getFocus(user.getId(), userId) != null) {
                        user.setIsFocus(2);
                    }
                }

                Events events = eventsDao.getNewestEventsByUserId(user.getId());
                StartsVo startsVo = new StartsVo();
                startsVo.setAppUser(user);
                startsVo.setNewestEvents(events);
                resultList.add(startsVo);
            }
            resultMap.put("resultList", resultList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    @RequestMapping(value = "/userEventList.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> userEventList(
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            AppUser user = userDao.getUserById(userId);

            List<Events> eventsList = eventsDao.getEventsByAppUserId(userId,0,100);
            resultMap.put("user", user);
            resultMap.put("eventsList", eventsList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 发现好友
     *
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/interested.json")
    @ResponseBody
    public Map<String, Object> interested(
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //最近给我点赞的2个人
            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
            List<Likes> likesList = likesDao.getLikesByAppUserId(userId);
            AppUser user = userDao.getUserById(userId);
            for (Likes likes : likesList) {
                //点赞人信息
                AppUser likeUser = userDao.getUserById(likes.getMyId());

                //获取点赞人粉丝数
                long fansCount = focusDao.getFansByIdCount(likeUser.getId());
                likeUser.setFansCount(fansCount);

                //是否已关注点赞人，未关注的显示，已关注的不进行显示
                if (focusDao.getFocus(userId, likeUser.getId()) == null) {
                    likeUser.setIsFocus(0);
                    Double distance = Common.getDistance(likeUser.getLongitude(), likeUser.getLatitude(), user.getLongitude(), user.getLatitude());
                    likeUser.setDistance(distance/1000);
                    Events events = eventsDao.getNewestEventsByUserId(likeUser.getId());
                    StartsVo startsVo = new StartsVo();
                    startsVo.setAppUser(likeUser);
                    startsVo.setNewestEvents(events);
                    startsVoList.add(startsVo);
                }
            }

            //最新分享我的2个人
            List<Share> shareList = shareDao.getShareByAppUserId(userId);
            for (Share share : shareList) {
                //点赞人信息
                AppUser shareUser = userDao.getUserById(share.getMyId());

                //获取点赞人粉丝数
                long fansCount = focusDao.getFansByIdCount(shareUser.getId());
                shareUser.setFansCount(fansCount);

                //是否已关注点赞人，未关注的显示，已关注的不进行显示
                if (focusDao.getFocus(userId, shareUser.getId()) == null) {
                    shareUser.setIsFocus(0);

                    Double distance = Common.getDistance(shareUser.getLongitude(), shareUser.getLatitude(), user.getLongitude(), user.getLatitude());
                    shareUser.setDistance(distance/1000);

                    Events events = eventsDao.getNewestEventsByUserId(shareUser.getId());
                    StartsVo startsVo = new StartsVo();
                    startsVo.setAppUser(shareUser);
                    startsVo.setNewestEvents(events);
                    startsVoList.add(startsVo);
                }
            }


            /**
             * 我最新发布的2个活动的最新评论的5个用户
             */
            List<Events> eventsList = eventsDao.getEventsByAppUserId(userId,0,2);
            for (Events events : eventsList) {
                //获取活动最近的回复
                List<EventComments> eventCommentsList = eventCommentsDao.getCommentsListByEventId2(events.getId());
                for (EventComments eventComments:eventCommentsList){
                    AppUser commentUser = userDao.getUserById(eventComments.getAppUserId());

                    long fansCount = focusDao.getFansByIdCount(commentUser.getId());
                    commentUser.setFansCount(fansCount);

                    //是否已关注评论人，未关注的显示，已关注的不进行显示
                    if (focusDao.getFocus(userId, commentUser.getId()) == null) {
                        commentUser.setIsFocus(0);

                        Double distance = Common.getDistance(commentUser.getLongitude(), commentUser.getLatitude(), user.getLongitude(), user.getLatitude());
                        commentUser.setDistance(distance/1000);

                        Events events1 = eventsDao.getNewestEventsByUserId(commentUser.getId());
                        StartsVo startsVo = new StartsVo();
                        startsVo.setAppUser(commentUser);
                        startsVo.setNewestEvents(events1);
                        startsVoList.add(startsVo);
                    }
                }

            }


            Set<StartsVo> ts = new HashSet<StartsVo>();
            ts.addAll(startsVoList);

            resultMap.put("startsVoList",ts);
            resultMap.put("result",true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 活动图片
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/eventsImageList" )
    @ResponseBody
    public Map<String, Object> eventsImageList(
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            List<Image> imageList = imageDao.getImageByEventsId(eventsId);

            Collections.sort(imageList, new Comparator<Image>() {
                @Override
                public int compare(Image o1, Image o2) {
                    return o1.getTokeTime().compareTo(o2.getTokeTime());
                }
            });
            resultMap.put("imageList",imageList);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 我在现场
     * @param userId
     * @param eventsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/inEventsList" )
    @ResponseBody
    public Map<String, Object> inEventsList(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "pageNum",defaultValue = "0",required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
            Events events = eventsDao.getEventsById(eventsId);
            List<AppUser> userList = userDao.getAllList();
            for (AppUser user:userList){
                Double distance = (Common.getDistance(user.getLongitude(), user.getLatitude(), events.getLongitude(),events.getLatitude()))/1000;
                user.setDistance(distance);
                if (distance<=1){
                    //粉丝数
                    long fansCount = focusDao.getFansByIdCount(user.getId());
                    user.setFansCount(fansCount);

                    //是否已关注红人
                    if (focusDao.getFocus(userId, user.getId()) == null) {
                        user.setIsFocus(0);
                    } else {
                        user.setIsFocus(1);
                        if (focusDao.getFocus(user.getId(), userId) != null) {
                            user.setIsFocus(2);
                        }
                    }

                    Events newestEvents = eventsDao.getNewestEventsByUserId(user.getId());
                    StartsVo startsVo = new StartsVo();
                    startsVo.setAppUser(user);
                    startsVo.setNewestEvents(newestEvents);
                    startsVoList.add(startsVo);
                }
            }
            resultMap.put("startsVoList",startsVoList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



}
